require(['gaodeMap'], function () {
    var stationMarkers = [],
        tmpUpStopNameAr = [],
        tmpDownStopNameAr = [],
        polygonOrCircle,
        infoWindow,
        areaAr = [];

    window.showData = function (lineInfo) {
        //console.log(lineInfo);

        map.clearMap();
        lineSearch(lineInfo.city, lineInfo.name, lineInfo.province);

        //取线路下的黑点进行展示
        $.ajaxExt({
            url: "/baseLineBlackRel/getBlackSpotByLineId.htm",
            data: {lineId: lineInfo.id}
        }).done(function (list) {
            //console.log(list);

            //debugger;

            $.each(areaAr, function (i, item) {
                item.setMap(null);
            });
            areaAr = [];

            if (infoWindow) {
                infoWindow.setMap(null);
            }

            if (list) {
                var data;
                for (var i = 0; i < list.length; i++) {
                    data = list[i];

                    //console.log(data);

                    var params = [];
                    if (data.fencType == 1) {
                        var item;
                        for (var j = 0; j < data.list.length; j++) {
                            item = data.list[j];
                            params.push([item.fencLgt, item.fencLtt]);
                            if (j == 0) {
                                //map.setMapCenterAndLevel(item.fencLgt, item.fencLtt, 12);
                            }
                        }

                        polygonOrCircle = new AMap.Polygon($.extend({
                            map: map,
                            path: params
                        }, defaultConfig));

                        polygonOrCircle.on('mouseover', markerClick);
                        var areaRoad = data.hasOwnProperty('areaRoad') == true ? data.areaRoad : '未知';
                        polygonOrCircle.content = '<p>黑点名称：' + data.blackName + '</p>' +
                            '<p>黑点性质：' + data.blackNature + '</p><p>危险度：' + data.dangerDegree + '</p><p>区域路段：' + areaRoad + '</p>';
                        polygonOrCircle.position = params[0];


                    } else {
                        var center = [data.coreLgt, data.coreLtt];
                        polygonOrCircle = new AMap.Circle($.extend({
                            center: center,// 圆心位置
                            radius: data.coreRadius
                        }, defaultConfig));
                        //map.setMapCenterAndLevel(data.coreLgt, data.coreLtt, 12);

                        polygonOrCircle.on('mouseover', markerClick);
                        var areaRoad = data.hasOwnProperty('areaRoad') == true ? data.areaRoad : '未知';
                        polygonOrCircle.content = '<p>黑点名称：' + data.blackName + '</p>' +
                            '<p>黑点性质：' + data.blackNature + '</p><p>危险度：' + data.dangerDegree + '</p><p>区域路段：' + areaRoad + '</p>';
                        polygonOrCircle.position = center;


                    }
                    polygonOrCircle.setMap(map);
                    areaAr.push(polygonOrCircle);
                }
            }
            mapObj.mapFitView();
            showMarkerInfo(areaAr[0]);


        });


    }

    //地图设置开始  ----------------------------------------------------
    
    // 动态设置高度
    $('#iCenter').height($(window.parent).height() * 0.85);

    //地图
    var mapObj = new MapGaoDe(107.49023438, 36.24427318, 4);
    map = mapObj.map;

    var defaultConfig = {
        strokeColor: "#ff0000",
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: "#ff0000",
        fillOpacity: 0.35,
        strokeStyle: "solid",
        strokeDasharray: [10, 5]
    };

    /**
     * 标记点击显示信息
     * @param e 目标元素
     */
    function markerClick(e) {
        if (infoWindow) {
            infoWindow.setMap(null);
        }
        infoWindow = new AMap.InfoWindow({
            content: e.target.content
        });
        infoWindow.open(map, e.target.position);
    }

    /**
     * 显示标记信息
     * @param circle
     */
    function showMarkerInfo(areaAr) {
        if (infoWindow) {
            infoWindow.setMap(null);
        }
        infoWindow = new AMap.InfoWindow({
            content: areaAr.content
        });
        infoWindow.open(map, areaAr.position);
    }

    //地图设置结束  ----------------------------------------------------

    //画线路开始  ----------------------------------------------------

    /**
     * 公交线路查询
     * @param city 城市名称
     * @param lineName 线路名称
     */
    function lineSearch(city, lineName, province) {
        var loct = city;
        if (province == '北京' || province == '上海' ||
            province == '天津' || province == '重庆') {
            loct = province;
        }
        /*
         * 1. 首先调用公交路线查询服务(lineSearch)
         * 2. 根据返回结果解析，输出解析结果(lineSearch_Callback)
         * 3. 在地图上绘制公交线路()
         */
        var linesearch = new AMap.LineSearch({
            pageIndex: 1,
            city: loct,
            pageSize: 2,
            extensions: 'all'
        });
        if (!linesearch) {
            map.setCity(city);
        }
        linesearch.search(lineName, function (status, result) {
            if (status === 'complete' && result.info === 'OK') {
                lineSearch_Callback(result);
                var lineArr = result.lineInfo;
                var stops = lineArr[0].via_stops;
                var stationsLen = lineArr[0].via_stops.length;
                for (var i = 0; i < stationsLen; i++) {
                    stationMarkers.push(new AMap.Marker({
                        map: map,
                        position: lineArr[0].via_stops[i].location,
                        offset: new AMap.Pixel(-3, -3),
                        icon: $.getProjectName() + "/content/images/way_btn2.png",
                        title: lineArr[0].via_stops[i].name,
                        zIndex: 13
                    }));
                    //TODO
                }

                for (var i = 0; i < lineArr[0].via_stops.length; i++) {
                    tmpUpStopNameAr.push(lineArr[0].via_stops[i].name);
                }
                if (lineArr.length > 1) {
                    for (var i = 0; i < lineArr[1].via_stops.length; i++) {
                        tmpDownStopNameAr.push(lineArr[1].via_stops[i].name);
                    }
                } else {
                    for (var i = lineArr[0].via_stops.length - 1; i >= 0; i--) {
                        tmpDownStopNameAr.push(lineArr[0].via_stops[i].name);
                    }
                }
                //map.setCity(city);
            }
            else {
                map.setCity(city);
            }
        });
    }

    /**
     * 公交路线查询服务返回数据解析概况
     * @param data 线路数据集合（包括绘制线路的基点经纬度数据）
     */
    function lineSearch_Callback(data) {
        var lineArr = data.lineInfo;
        var lineNum = data.lineInfo.length;
        if (lineNum == 0) {
        } else if (lineNum == 1) {
            for (var i = 0; i < lineNum; i++) {
                var pathArr = lineArr[i].path;
                var stops = lineArr[i].via_stops;
                var startPot = stops[0].location;
                var endPot = stops[stops.length - 1].location;
                if (i == 0)
                    drawbusLine(startPot, endPot, pathArr);
            }
        } else {
            for (var i = 0; i < lineNum - 1; i++) {
                var pathArr = lineArr[i].path;
                var stops = lineArr[i].via_stops;
                var startPot = stops[0].location;
                var endPot = stops[stops.length - 1].location;
                if (i == 0)
                    drawbusLine(startPot, endPot, pathArr);
            }
        }
    }

    /**
     * 绘制路线
     * @param startPot 开始站点位置信息
     * @param endPot 结束站点位置信息
     * @param BusArr 公交线路基点经纬度集合
     */
    function drawbusLine(startPot, endPot, BusArr) {
        //绘制起点，终点
        new AMap.Marker({
            map: map,
            position: [startPot.lng, startPot.lat], //基点位置
            icon: $.getProjectName() + "/content/images/start_flag.png",
            zIndex: 10
        });
        new AMap.Marker({
            map: map,
            position: [endPot.lng, endPot.lat], //基点位置
            icon: $.getProjectName() + "/content/images/end_flag.png",
            zIndex: 10
        });
        //绘制乘车的路线
        busPolyline = new AMap.Polyline({
            map: map,
            path: BusArr,
            strokeColor: "#09f",//线颜色
            strokeOpacity: 0.8,//线透明度
            strokeWeight: 6//线宽
        });
        map.setFitView();
        //绘制
    }

    //画线路结束  ----------------------------------------------------

});